# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/4/15
# @File   : Rule29_Ind_UnpaidGuaranClass5Sate_Check.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人未结清对外担保五级分类校验
code:Ind_UnpaidGuaranClass5Sate_Check_2D
tableName:
columnName:
groups:准入>个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys


reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


#相关还款责任信息  PCR  [1..1]  相关还款责任信息单元  PD03  [0..*]
# 相关还款责任信息段  PD03A  [1..1]  余额  PD03AJ02  [1..1]
# 五级分类  PD03AD05  [1..1]

def Ind_UnpaidGuaranClass5Sate_Check():
    data = IC_DSJ_2D['data']
    PCR = getTable(data, 'PCR', 1)
    if PCR is None:
        return u'缺失值'
    elif PCR == [{}] or PCR == "":
        return 0
    else:
        PD03 = getTable(PCR[0], 'PD03', 0)
        if PD03 is None or PD03 == "":
            return None
        elif PD03 == [{}]:
            return 0
        else:
            result = []
            for p2 in PD03:
                try:
                    PD03A = getTable(p2, 'PD03A', 1)
                    if PD03A in null_type_list:
                        result.append(None)
                    else:
                        balance = PD03A[0].get('PD03AJ02')
                        fiveClassState = PD03A[0].get('PD03AD05')
                        if isfloat(balance) and fiveClassState not in null_type_list:
                            if fiveClassState in ['2', '3', '4', '5']:
                                result.append(float(balance))
                            else:
                                result.append(0)
                        else:
                            result.append(None)
                except:
                    result.append(None)
            result = filter(lambda x: x is not None, result)
            if len(result) == 0:
                return u'缺失值'
            else:
                return sum(result)


result = Ind_UnpaidGuaranClass5Sate_Check()

